package com.touch.jgloo;
import javax.media.opengl.*;

import com.touch.jgloo.core.JGLBase;
/**
 * Sets up the view for a 2D Ortho view.
 * @author HomicidalMonkey
 * @version 10.1.9
 * @since r461
 * @see com/touch/Licence
 */
public class JGLOrthoView implements GLEventListener
{		
	private final double d1, d2, d3, d4;
	public JGLOrthoView()
	{
		this.d1 = 0d;
		this.d2 = 640d;
		this.d3 = 480d;
		this.d4 = 0d;
	}
	public JGLOrthoView(double d1, double d2, double d3, double d4)
	{
		this.d1 = d1;
		this.d2 = d2;
		this.d3 = d3;
		this.d4 = d4;
	}
	public void display(GLAutoDrawable drawable) 
	{	
		//get the GL object
		final GL gl = drawable.getGL();
		//load the GL identity matrix
		gl.glLoadIdentity();
		//clear the buffer.
		gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);		
	}
	public void displayChanged(GLAutoDrawable arg0, boolean arg1, boolean arg2) {}
	public void init(GLAutoDrawable drawable) 
	{
		//get the GL object
		final GL gl = drawable.getGL();
		
		//set the shading mode
		gl.glShadeModel(GL.GL_SMOOTH);
		//Set the background color to black
		gl.glClearColor(0f, 0f, 0f, 0f);
		//Set the depth to 1f
		gl.glClearDepth(1.0f);
		//Enable the use of the z-axis
		gl.glEnable(GL.GL_DEPTH_TEST);	
		//perform LEQUAL depth test
		gl.glDepthFunc(GL.GL_LEQUAL);  
		//Enable using textures
		gl.glEnable(GL.GL_TEXTURE_2D);
		//Enable color blending
		gl.glEnable(GL.GL_BLEND);
		//Enable alpha blending
		gl.glEnable(GL.GL_ALPHA_TEST);
		//set Alpha function
		gl.glAlphaFunc(GL.GL_GREATER, 0);	
		//more high-quality
		gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
		//more high-quality
		gl.glHint(GL.GL_POLYGON_SMOOTH_HINT, GL.GL_NICEST);		
		JGLBase.GL_INSTANCE = gl;
	}
	public void reshape(GLAutoDrawable drawable, final int x, final int y, final int width, final int height)  
	{
		//get the GL Object
		final GL gl = drawable.getGL();		
		//only draw when the screen re-syncs
		gl.setSwapInterval(1);
		//set the view size of the GL instance
		gl.glViewport(0, 0, width, height);
		//swich to projection mode
		gl.glMatrixMode(GL.GL_PROJECTION);
		//load the identity matrix
		gl.glLoadIdentity();
		//define the dimensions (600x480x50)
		gl.glOrtho(d1, d2, d3, d4, 0d, 50d);
		//switch back to model mode
		gl.glMatrixMode(GL.GL_MODELVIEW);
		//reload the identity matrix
		gl.glLoadIdentity();			
	}	
}
